package com.qf.service.impl;

import com.qf.dao.LoansDao;
import com.qf.dao.LogDao;
import com.qf.dao.ProductDao;
import com.qf.dao.UserDao;
import com.qf.dao.impl.LoansDaoImpl;
import com.qf.dao.impl.LogDaoImpl;
import com.qf.dao.impl.ProductDaoImpl;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.model.*;
import com.qf.model.pojo.Userloans;
import com.qf.service.LoansService;
import com.qf.utils.DruidUtil;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.List;

public class LoansServiceImpl implements LoansService {
    private LoansDao loansDao=new LoansDaoImpl();
    private ProductDao productDao = new ProductDaoImpl();
    private UserDao userDao=new UserDaoImpl();
    private LogDao logDao=new LogDaoImpl();
    @Override
    public List<Loans> findAll() {
        return loansDao.findAll();
    }

    @Override
    public int addMyLoans(Integer l_id, Integer u_id, Double quota, String c_id) {
        return loansDao.addMyLoans(l_id,u_id,quota,c_id);
    }
    //管理员查询所有待审批贷款
    @Override
    public List<Userloans> findAllMyLoans(PageBean<Userloans> pageBean) {
        return loansDao.findAllMyLoans(pageBean);
    }
    //管理员拒绝贷款审批

    @Override
    public int sUpdateMyLoans(int l_id) {
        return loansDao.sUpdateMyLoans(l_id);
    }

    //根据l_id查询贷款产品信息

    @Override
    public Loans findLoans(int l_id) {
        return loansDao.findLoans(l_id);
    }
    //根据i_id查询贷款信息


    @Override
    public MyLoans findMyLoans(int l_id) {
        return loansDao.findMyLoans(l_id);
    }

    //管理员审批贷款   事务
    //1，修改我的贷款
    //2.转账到收款账户
    //3.扣款贷款产品余额
    //4.添加日志
    @Override
    public int UpdateMyLoans(int l_id) {
        //开启事务
        Connection conn = DruidUtil.getConnection();
        DruidUtil.begin(conn);

        //1.查询贷款信息
        MyLoans myLoans = loansDao.findMyLoans(l_id);
        if(myLoans==null){
            return 0;
        }else {
            //1.1根据用户查询用户信息
            User user = userDao.findByUid(myLoans.getU_id());
            //2.转账到贷款人银行卡上
            int i = productDao.addCardMoney(conn, myLoans.getC_id(), myLoans.getQuota());
            //3.修改贷款信息状态
            int i2 = loansDao.updateMyLoans(conn, myLoans.getId());
            //4.添加贷款人银行卡收款日志
            //添加日志信息
            Log log = new Log(
                    user.getU_idcard(),
                    user.getU_phone(),
                    myLoans.getC_id(),
                    myLoans.getQuota(),
                    2,
                    new Timestamp(new java.util.Date().getTime()),
                    4,
                    user.getU_name()+"贷款入账"+myLoans.getQuota()+"元"
            );
            //4.1添加日志信息
            int i3 = logDao.addLog(conn, log);
            //5.贷款产品扣款
            int i1 = productDao.lessLoansMoney(conn, myLoans.getL_id(), myLoans.getQuota());
            //6.日志信息  不写
            if(i==1&&i1==1&&i2==1&&i3==1){
                //提交事务
                DruidUtil.commit(conn);
                return 1;
            }else {
                DruidUtil.rollback(conn);
                return 0;
            }
        }

    }
}
